package biz.datalk.industrialland.common.log.config;

import biz.datalk.industrialland.common.log.mapper.ISysLogExtMapper;
import biz.datalk.industrialland.common.log.mapper.ISysLogMapper;
import biz.datalk.industrialland.common.log.service.ISysLogService;
import biz.datalk.industrialland.common.log.service.impl.SysLogServiceDefaultImpl;
import biz.datalk.industrialland.common.log.service.impl.SysLogServiceMongoImpl;
import biz.datalk.industrialland.common.log.service.impl.SysLogServiceMySQLImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author tarofang@163.com
 * @date 2020-11-24
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(LogProperties.class)
public class SysLogConfig {

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnProperty(name = "datalk-log.enable", havingValue = "true", matchIfMissing = true)
    protected static class SysLogEnableConfig {
        @Bean
        @ConditionalOnProperty(name = "datalk-log.log-save-db-type", havingValue = "1")
        public ISysLogService sysLogServiceMySQL( //
                LogProperties logProperties, //
                ISysLogMapper sysLogMapper, //
                ISysLogExtMapper sysLogExtMapper, @Qualifier("logTaskThreadPoolExecutor") ThreadPoolTaskExecutor logTaskThreadPoolExecutor) {
            log.debug("ISysLogService init ====================== S");
            try {
                SysLogServiceMySQLImpl sysLogServiceMySQL = new SysLogServiceMySQLImpl(logProperties.getLogServerIp(), logProperties.getServerPrivateKey(), logTaskThreadPoolExecutor, sysLogMapper, sysLogExtMapper);
                log.info("Implements class {}", sysLogServiceMySQL.getClass().getName());
                return sysLogServiceMySQL;
            } finally {
                log.debug("ISysLogService init ====================== E");
            }
        }

        @Bean
        @ConditionalOnProperty(name = "datalk-log.log-save-db-type", havingValue = "2", matchIfMissing = true)
        public ISysLogService sysLogServiceMongo(
                MongoTemplate mongoTemplate, //
                LogProperties logProperties, //
                @Qualifier("logTaskThreadPoolExecutor") ThreadPoolTaskExecutor logTaskThreadPoolExecutor) {
            log.debug("ISysLogService init ====================== S");
            try {
                SysLogServiceMongoImpl sysLogServiceMongo = new SysLogServiceMongoImpl(mongoTemplate, logProperties.getLogServerIp(), logProperties.getServerPrivateKey(), logTaskThreadPoolExecutor);
                log.info("Implements class {}", sysLogServiceMongo.getClass().getName());
                return sysLogServiceMongo;
            } finally {
                log.debug("ISysLogService init ====================== E");
            }
        }
    }

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnProperty(name = "datalk-log.enable", havingValue = "false")
    protected static class SysLogDisableConfig {
        @Bean
        @ConditionalOnMissingBean
        public ISysLogService sysLogServiceDefault(LogProperties logProperties) {
            log.debug("ISysLogService init ====================== S");
            try {
                SysLogServiceDefaultImpl logService = new SysLogServiceDefaultImpl(logProperties);
                log.info("Implements class {}", logService.getClass().getName());
                return logService;
            } finally {
                log.debug("ISysLogService init ====================== E");
            }
        }
    }

}
